Skip to content

add support for two-digit year in yeear selection (63095) #258

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

svenwey
Copy link
Collaborator

@svenwey svenwey commented Apr 11, 2025

This is a bit hacky and not really clean in the general case (e.g. if used with a potential US format). However, as the 2-digit format itself is not really clean, I can't think of a better approach.

@svenwey svenwey self-assigned this Apr 11, 2025
Comment on lines 130 to 160
def parse_date(date)
return nil if date.blank?

if date.is_a? String
if date.is_a?(String)
if date.match?(/\A\d{1,2}\.\d{1,2}\.\d{2}\z/)
day, month, short_year = date.split('.').map(&:to_i)

# Get current full year and its century
current_year = Time.zone.today.year
cutoff = (current_year + 5) % 100
current_century = (current_year / 100) * 100

full_year = if short_year <= cutoff
current_century + short_year
else
(current_century - 100) + short_year
end

return Date.new(full_year, month, day)
end

begin
date = Date.strptime(date, I18n.t('date.formats.default'))
rescue StandardError
rescue ArgumentError
date = Date.parse(date)
end
end
date = date.to_date if date.is_a? Time

date = date.to_date if date.is_a?(Time)
date
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

strptime can handle the parsing of two year dates. Locale specific formats are handled in Rails with the I18n gem.

I'd rewrite this method as:

    def parse_date(date)
      return nil if date.blank?

      case date
      when Time then date.to_date
      when /\A\d{1,2}\.\d{1,2}\.\d{2}\z/ then parse_date_string(date, :short_year)
      when String then parse_date_string(date)
      else date
      end
    end

    private

    def parse_date_string(date, type=:default)
      Date.strptime(date, I18n.t(type, scope: 'date.formats'))
    rescue ArgumentError
      Date.parse(date)
    end
...

and add the following to config/locales/crud.de-CH.yml:

de-CH:
  ...
  # formats
  date:
    formats:
      short_year: "%d.%m.%y"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants